
package scalaExec.scalaLab;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import scalaExec.Interpreter.GlobalValues;

public class initScalaConsole {
     public String compFile;
     public String libFile;
     public String swingFile;
     public String libsFile;
     

    URL  jarPathOfClass(String className) {
        try {
            return Class.forName(className).getProtectionDomain().getCodeSource().getLocation();
        } catch (ClassNotFoundException ex) {
           System.out.println("error in jarPathOfClass"+className+")");
           ex.printStackTrace();
           return null;
        }
}
    
      public static String decodeFileName(String fileName) {
        String decodedFile = fileName;

        try {
            decodedFile = URLDecoder.decode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            System.err.println("Encounted an invalid encoding scheme when trying to use URLDecoder.decode().");
            System.err.println("Please note that if you encounter this error and you have spaces in your directory you will run into issues. ");
        }

        return decodedFile;
    }
      
    public void scalaLabScalaConsole()
    {
        boolean      hostIsUnix = true;
        
             String jarFile = GlobalValues.jarFilePath;
         if (File.pathSeparatorChar!=';') {   // host is Unix   
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "/");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","/"); 
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "/");
       libsFile = jarPathOfClass("DhbEstimation.LinearRegression").toString().replace("file:/", "/");
                    } 
         else {
       compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "");
       libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","");
       swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "");
       libsFile = jarPathOfClass("DhbEstimation.LinearRegression").toString().replace("file:/", "");
                  }   

             String [] args = new String [2];
             args[0] = "-classpath";
             args[1] =  "."+File.pathSeparator+GlobalValues.jarFilePath+File.pathSeparator+compFile+
                         File.pathSeparator+libFile+
                         File.pathSeparator+swingFile+
                         File.pathSeparator+libsFile;
      
             scala.tools.nsc.MainGenericRunner.main(args);
             
    }
                  
}
